/**
  addChild(new TxtBtn("test",clickfun));
 */
package
{
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.text.TextFormat;
	import flash.text.TextField;
	import flash.display.Shape;
	import flash.geom.Rectangle;
	public class TxtBtn extends Sprite
	{
		private var cur_obj:Object=null;
		private var font_size:uint = 0;
		public function TxtBtn(s:String,f:Function,_cur_obj:Object=null,_font_size:uint=0)
		{
			name = s;
			/*mouseChildren = false;*/
			cur_obj = _cur_obj;
			func = f;
			font_size = _font_size;
			txtstr = (s);
			if(stage)init();
			else addEventListener(Event.ADDED_TO_STAGE,init);
		}
		public function set_fun(f:Function,_cur_obj:Object =null):void
		{
			cur_obj = _cur_obj;
			func = f;
		}
		public function get text():String
		{
			return txtstr;
		}
		public function set text(s:String):void
		{
			txtstr = (s);
			if(btntxt1)btntxt1.text = txtstr;
			if(btntxt2)btntxt2.text = txtstr;
		}
		private function init(e:Event = null):void
		{
			if(_width == 0)setformat();
			var btn:BtnBase = makebtn(txtstr,func,cur_obj);
			/*trace(txtstr);*/
			addChild(btn);
		}

		private var func:Function;
		private var txtstr:String;
		private var _width:int;
		private var _height:int;
		private var bgcolor1:uint=0x201212;
		private var bgcolor2:uint=0x201212;
		private var txt1color:uint=0xffffff;
		private var txt2color:uint=0xffffff;
		private var border_color:uint=0xffffff;
		public function setformat(rect:Rectangle=null,_bgcolor1:uint=0x201212,_bgcolor2:uint=0x201212,_txt1color:uint=0xffffff,_txt2color:uint=0xffffff,_border_color:uint=0x000000):void
		{
			if(rect)
			{
				x = rect.x;
				y = rect.y;
				_width = rect.width;
				_height = rect.height;
			}

			if(_width == 0)_width = 497;
			if(_height==0)_height = 90;

			bgcolor1 = _bgcolor1;
			bgcolor2 = _bgcolor2;
			txt1color = _txt1color;
			txt2color = _txt2color;
			border_color = _border_color;
		}

		private function makebtnbg(_color:uint):Shape
		{
			var shape:Shape = new Shape();
			var round_radio :int = Math.min(_height/10,_width/10);
			with(shape.graphics)
			{
				beginFill(_color);
				lineStyle(1,border_color);
				drawRoundRect(0,0, _width, _height,round_radio,round_radio);
				endFill();
			}
			return shape;
		}
		private function makebtntxt(str:String,_color:uint,_size:int):TextField
		{
			var txt:TextField = ViewSet.makeTxtLine(str,_size,_color);
			txt.x = _width/2-txt.textWidth/2;
			txt.y = _height /2 - txt.textHeight/2;
			return txt;
		}
		private var btntxt1:TextField;
		private var btntxt2:TextField;
		private function makebtn(str:String,fun:Function,_paras:Object=null):BtnBase
		{
			/*str=Fanti.jian2fan(str);*/
			var _font_size:int = _height*.5;
			if(font_size>0){
				_font_size = font_size;
			}else{
				_font_size= _height*.5;
			}
			var btnbg1:Shape = makebtnbg(bgcolor1);
			var btnbg2:Shape = makebtnbg(bgcolor2);
			btntxt1= makebtntxt(str,txt1color,_font_size);
			btntxt2= makebtntxt(str,txt2color,_font_size);

			var btn_on:Sprite = new Sprite();
			var btn_out:Sprite = new Sprite();

			btn_on.addChild(btnbg1);
			btn_out.addChild(btnbg2);
			btn_on.addChild(btntxt1);
			btn_out.addChild(btntxt2);
			var btn:BtnBase = new BtnBase(fun,btn_out,btn_on,_paras);
			return btn;
		}
	}
}

